package leecode.q84;


import java.util.Stack;

public class Solution {
    int maxArea = 0;

    public int largestRectangleArea(int[] heights) {
        for (int i = 0; i < heights.length; i++) {
            stack.clear();
            calcCurMaxArea(i, heights);
        }
        return maxArea;
    }

    private void calcCurMaxArea(int i, int[] heights) {
        int area;
        for (int j = i; j < heights.length; j++) {
            while (!stack.isEmpty() && heights[j] < stack.peek()) {
                System.out.println("");
                stack.pop();
            }
            stack.push(heights[j]);
        }
    }

    // 73,74,75,71,69,72,76,73
    // 1,1,4,2,1,1,0,0
    // 30,40,50,60
    public int[] dailyTemperatures1(int[] temperatures) {
        res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            calcTemp(i, temperatures);
        }
        return res;
    }

    private void calcTemp(int i, int[] temperatures) {
        for (int j = i; j < temperatures.length; j++) {
            if (temperatures[i] < temperatures[j]) {
                res[i] = j - i;
                return;
            }
        }
        res[i] = 0;
    }

    Stack<Integer> stack = new Stack<>();
    int[] res;

    public int[] dailyTemperatures(int[] temperatures) {
        res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            if (stack.empty()) {
                stack.push(i);
            } else if (temperatures[i] <= temperatures[stack.peek()]) {
                stack.push(i);
            } else {
                while (!stack.empty() && temperatures[i] > temperatures[stack.peek()]) {
                    Integer index = stack.pop();
                    res[index] = i - index;
                }
                stack.push(i);
            }
        }
        return res;
    }
}
